package cn.mymall.help;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 用于接收service分页功能返回，以及api接口返回前端
 * @param <T>
 */
public class PageVo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 总数
     */
    private Long total;

    /**
     * 每页显示条数，默认 10
     */
    private Long pageSize = 20L;

    /**
     * 当前页
     */
    private Long pageNo = 1L;

    private Long pages;

    /**
     * 查询数据列表
     */
    private List<T> records = Collections.emptyList();

    public Long getTotal() {
        return total;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public long getPages() {
        return (total-1)/pageSize + 1;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNo(Long pageNo) {
        this.pageNo = pageNo;
    }

    public Long getPageNo() {
        return pageNo;
    }

    public void setPages(Long pages) {
        this.pages = pages;
    }

    public void setTotal(Long total) {
        this.total = total;
    }


    public static PageVo getInstance(Page page){
        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setPageNo(page.getCurrent());
        pageVo.setRecords(page.getRecords());
        pageVo.setPageSize(page.getSize());
        return pageVo;
    }

}
